/**
 * 基本工具类
 * @author  Jello
 * @create  2018/10/15 16:21
 * @version 1.0
 * @company 广东蓝聘科技网络有限公司
 **/

/**
 * 截取请求链接上参数(http://192.168.1.108:8080/bluehired/login.html?identity=45670b55437d4cbd920504d296fd3398)
 * @param {Object} param 如identity
 */
function getParam(param) {
	//获取当前URL
	var local_url = document.location.href;
	//获取要取得的get参数位置
	var get = local_url.indexOf(param + "=");
	if(get == -1) {
		return null;
	}
	//截取字符串
	var get_par = local_url.slice(param.length + get + 1);
	//判断截取后的字符串是否还有其他get参数
	var nextPar = get_par.indexOf("&");
	if(nextPar != -1) {
		get_par = get_par.slice(0, nextPar);
	}
	return get_par;
}

/**
 * 截取请求链接上参数
 * @param {Object} url 如http://192.168.1.108:8080/bluehired/login.html?identity=45670b55437d4cbd920504d296fd3398
 * @param {Object} param 如identity
 */
function getParamUrl(url, param) {
	//获取当前URL
	var local_url = url;
	//获取要取得的get参数位置
	var get = local_url.indexOf(param + "=");
	if(get == -1) {
		return null;
	}
	//截取字符串
	var get_par = local_url.slice(param.length + get + 1);
	//判断截取后的字符串是否还有其他get参数
	var nextPar = get_par.indexOf("&");
	if(nextPar != -1) {
		get_par = get_par.slice(0, nextPar);
	}
	return get_par
}

/**
 * 制保留2位小数，如：2会在后面补上00，即2.00 
 * @param {Object} number 数字
 */
function toDecimal(number) {   
	var f = parseFloat(number);   
	if(isNaN(f)) {    
		return false;   
	}   
	var f = Math.round(number * 100) / 100;   
	var s = f.toString();   
	var rs = s.indexOf('.');   
	if(rs < 0) {    
		rs = s.length;    
		s += '.';   
	}   
	while(s.length <= rs + 2) {    
		s += '0';   
	}   
	return s;  
}

/**
 * 检测用户是否登录，若没跳转到登录
 * @param {Object} sign 登录状态
 * @param {Object} url 地址
 * @param {Object} callback 回调
 */
function backLogin(sign, url, callback) {
	if(sign) {
		callback();
	} else {
		mui.toast("请先登录");
		setTimeout(function() {
			window.location.href = url;
		}, 800);
	}
}

/**
 * 跳转指定页面
 * @param {Object} sign 登录状态
 * @param {Object} target 目标页面
 * @param {Object} back
 */
function toPage(sign, target, back) {
	if(sign) {
		window.location.href = target;
	} else {
		mui.toast("请先登录");
		setTimeout(function() {
			window.location.href = back;
		}, 800);
	}
}

/**
 * 时间戳转化成时间格式(10/19 14:59)
 * @param {Object} timestamp 时间戳 
 */
function timeFormat(timestamp) {
	//timestamp是整数，否则要parseInt转换,不会出现少个0的情况
	var time = new Date(timestamp);
	var year = time.getFullYear();
	var month = time.getMonth() + 1;
	var date = time.getDate();
	var hours = time.getHours();
	var minutes = time.getMinutes();
	var seconds = time.getSeconds();
	return add0(month) + '/' + add0(date) + ' ' + add0(hours) + ':' + add0(minutes);
	//	return year + '-' + add0(month) + '-' + add0(date) + ' ' + add0(hours) + ':' + add0(minutes) + ':' + add0(seconds);
}

function add0(m) {
	return m < 10 ? '0' + m : m
}

/**
 * 时间戳转化成时间格式
 * @param {Object} timestamp 时间戳 (10-19 14:59)
 */
function timestampToTime(timestamp) {
	var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
	//      var Y = date.getFullYear() + '-';
	var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
	var D = date.getDate() + ' ';
	var h = date.getHours() + ':';
	var m = date.getMinutes() + '';
	//      var s = date.getSeconds();
	return M + D + h + m;
}

/**
 * 获取年月yyyy-mm
 */
function getYearMonth() {
	var now = new Date();
	var time = now.getFullYear() + "-" + ((now.getMonth() + 1) < 10 ? "0" : "") + (now.getMonth() + 1);
	return time;
}

/**
 * 获取年月日 yyyy-MM-dd
 */
function getNowFormatDate() {
	var date = new Date();
	var seperator1 = "-";
	var month = date.getMonth() + 1;
	var strDate = date.getDate();
	if(month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if(strDate >= 0 && strDate <= 9) {
		strDate = "0" + strDate;
	}
	var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;
	return currentdate;
}